<?php
require_once 'Admin/ViewModel.php';
require_once 'Admin/Model/UserModel.php';
class Controller{
	protected $model;
	protected $mod;
	protected $view;
	function __Construct(){
		$c = isset($_GET['c'])?$_GET['c']:'user';
		$m = isset($_GET['m'])?$_GET['m']:'index';
		$this->view = new ViewModel($c.'/'.$m);
	}	
	function index($mod){
		//$mod = ucfirst($mod).'Model';
		//$this->model= new $mod;
		/*$data = $this->model->getList();
		$this->assign('list',$data);*/
		$this->assign('list',$this->model->getList());
		$this->display();
	}
	function form($mod){
		$this->display();
	}
	function del($mod){
		$id = $_GET['id'];
		$result = $this->model->del($id);
		if($result){
			echo '删除成功 <a href="admin.php?c={$mod}&m=index">如果没有跳转，请点这里跳转</a>';
   			header("refresh:3;url='admin.php?c={$mod}&m=index'");
		}
		else{
			echo "删除失败！<br/>";
  			echo "<a href='admin.php?c={$mod}&m=index'>返回</a>";
		}
	}
	function add($mod){
		$data = $_POST;
		$result = $this->model->add($data);
		if($result){
			echo '添加成功 <a href="admin.php?c={$mod}&m=index">如果没有跳转，请点这里跳转</a>';
   			header("refresh:3;url='admin.php?c={$mod}&m=index'");
		}
		else{
			echo "添加失败！<br/>";
  			echo "<a href='javascript:history.back(-1)'>返回</a>";
		}

	}
	function mod($mod){
		$id = $_GET['id'];
		$result = $this->model->getOneByID($id);
		$this->assign('one',$result);
		$this->display(); 
	}
	function update($mod){
		$data = $_POST;
		$result = $this->model->update($data);
		if($result){
			echo '更新成功 <a href="admin.php?c={$mod}&m=index">如果没有跳转，请点这里跳转</a>';
   			header("refresh:3;url='admin.php?c={$mod}&m=index'");
		}
		else{
			echo "更新失败！<br/>";
  			echo "<a href='javascript:history.back(-1)'>返回</a>";
		}
	}
	function display() {
		$this->view->display();
	}
	function assign($variable, $value) {
		$this->view->assign($variable, $value);
	}
}
?>